using System.Linq;
using Volo.Abp.Localization;
using Volo.Abp.Authorization.Permissions;
using Joy.Erp.BasicArchives.Localization;

namespace Joy.Erp.BasicArchives.Permissions
{
    public class PlanOrderTypePermissionDefinitionPovider : PermissionDefinitionProvider
    {
        public override void Define(IPermissionDefinitionContext context)
        {
            var menus = context.GetGroupOrNull(BasicArchivesPermissions.Menus);
            if (menus == null)
            {
                menus = context.AddGroup(BasicArchivesPermissions.Menus, L("Permission:Menus"));
            }

            var productionManagementMenu = menus.GetPermissionOrNull(BasicArchivesPermissions.BasicArchivesMenu.ProductionManagement);
            if (productionManagementMenu == null)
            {
                productionManagementMenu = menus.AddPermission(BasicArchivesPermissions.BasicArchivesMenu.ProductionManagement, L("Permission:Menus.BasicArchives.ProductionManagement"));
            }

            var parentMenu = productionManagementMenu.Children.FirstOrDefault(x => x.Name == BasicArchivesPermissions.PlanOrderType.ParentMenu);
            if (parentMenu == null)
            {
                parentMenu = productionManagementMenu.AddChild(BasicArchivesPermissions.PlanOrderType.ParentMenu, L("Permission:Menus.BasicArchives.ProductionManagement.Production"));
            }

            var menu = parentMenu.Children.FirstOrDefault(x => x.Name == BasicArchivesPermissions.PlanOrderType.Menu);
            if (menu == null)
            {
                menu = parentMenu.AddChild(BasicArchivesPermissions.PlanOrderType.Menu, L("Permission:Menus.BasicArchives.ProductionManagement.Production.PlanOrderType"));
            }

            var group = context.GetGroupOrNull(BasicArchivesPermissions.GroupName);
            if (group == null)
            {
                group = context.AddGroup(BasicArchivesPermissions.GroupName, L("Permission:BasicArchives"));
            }

            var pPlanOrderTypeDefault = group.AddPermission(BasicArchivesPermissions.PlanOrderType.Default, L("Permission:BasicArchives.Production.PlanOrderType"));
            var pPlanOrderTypeQuery = pPlanOrderTypeDefault.AddChild(BasicArchivesPermissions.PlanOrderType.Query, L("Permission:Query"));
            var pPlanOrderTypeEdit = pPlanOrderTypeDefault.AddChild(BasicArchivesPermissions.PlanOrderType.Edit, L("Permission:Edit"));
            var pCreate = pPlanOrderTypeEdit.AddChild(BasicArchivesPermissions.PlanOrderType.Create, L("Permission:Create"));
            var pUpdate = pPlanOrderTypeEdit.AddChild(BasicArchivesPermissions.PlanOrderType.Update, L("Permission:Update"));
            var pDelete = pPlanOrderTypeEdit.AddChild(BasicArchivesPermissions.PlanOrderType.Delete, L("Permission:Delete"));
        }

        private static LocalizableString L(string name)
        {
            return LocalizableString.Create<BasicArchivesResource>(name);
        }
    }
}